<script>
import index from '@/api/index.js'

export default {
  name: "login-section",
  data() {
    return {
      email: '',
      password: '',
      loginMessage: "",
      userName:'登录',
    }
  },
  methods: {
    toPath(path) {
      // console.log(item)
      //   防止重复跳转路由
      if (this.$route.path !== path && !(this.$route.path === '/index' && (path === '/'))) {
        this.$router.push(path)
      }
    },
    login() {
      index.login(this.email, this.password).then((res) => {
        if (res.data.code === 200){
          this.$store.state.userName = res.data.data.userName
        }
        this.loginMessage = res.data.msg
      })
    }
  }
}
</script>

<template>
  <div class="login-section">
    <div class="login-top">登录<p @click="toPath('/index')" id="close-login">❎</p>
    </div>
    <div class="login-form">
      <h2 style="margin: 10px; margin-top: 30px;">用户登录</h2>
      <input type="text" id="email" name="email" placeholder="邮箱" v-model="email">
      <input type="password" id="password" name="password" placeholder="密码" v-model="password"/>
      <div class="login-menu">
        <div id="register"><a @click.prevent="toPath('/register')">免费注册</a></div>
        <div id="find-password">忘记密码</div>
      </div>
      <button type="button" @click="login" id="login-button">{{userName}}</button>
      <p id="reminder">{{ loginMessage }}</p>
      <h6 class="role">登录即代表同意《服务协议》和《隐私政策》</h6>
    </div>
  </div>
</template>

<style scoped>

#login {
  position: absolute;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.3);
  width: 100%;
  height: 100vh;
  display: none;
  align-items: center;
  /* transition: 1s; */
  opacity: 1;
  /* animation: loginShow 1s; */
  z-index: 99;
}

@keyframes loginShow {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes loginClose {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

.login-section {
  width: 400px;
  height: 400px;
  border-radius: 20px;
  background-color: #fff;
  margin: 0 auto;
  /* padding: 10px; */

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  z-index: 100;

}

.login-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
  box-sizing: border-box;
  align-items: center;
}

.login-form {
  width: 100%;
  border-top: 1px solid #c8c8c8;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 20px;
  flex-grow: 1;
}

.login-form input {
  border: 1px solid #c8c8c8;
  height: 32px;
  width: 300px;
  margin: 5px 0;
  line-height: 32px;
  padding: 0 10px;
  border-radius: 10px;
  transition: 0.2s;
  box-sizing: border-box;
}

.login-form input:hover {
  box-shadow: 0 0 2px 1px #00d200;
}

.login-menu {
  display: flex;
  justify-content: space-between;
  width: 300px;
}

.login-menu div {
  font-size: 12px;
}

.login-menu div:hover {
  color: #009d2c;
  cursor: pointer;
}

#login-button {
  width: 300px;
  background-color: #009d2c;
  height: 40px;
  border: none;
  border-radius: 10px;
  color: #fff;
  font-weight: bolder;
  margin: 30px 0;
  /* flex-grow: 1; */
  margin-bottom: 0;
  font-size: 22px;
}

#login-button:hover {
  cursor: pointer;
}

#reminder {
  font-size: 12px;
  flex-grow: 1;
  color: red;
}

.role {
  margin: 0.625rem;
}

#close-login {
  user-select: none;
  font-size: 25px;
}

#close-login:hover {
  cursor: pointer;
}


</style>